﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Web.Security;

public partial class Account_ManageLeague : System.Web.UI.Page
{
    User user;
    JsonParser jsonParser;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_Init(object sender, EventArgs e)
    {

        jsonParser = new JsonParser();

        user = (User)Session["UserSession"];

        Createleague_btn.Click += new EventHandler(this.createLeague_Click);

        try
        {
            UserTeam team = jsonParser.getUserTeam(user.username);
            if (team.leagueName != null)
            {
                Response.Redirect("~/Account/Profile.aspx");
            }

        }
        catch (Exception ex)
        {
            Response.Redirect("~/Account/NewTeam.aspx");
        }
    }

    protected void createLeague_Click(Object sender, EventArgs e)
    {
        if (user != null)
        {
            string name = "";
            if (!leaguename.Text.Equals(""))
            {
                name = leaguename.Text;
                bool leagueCreated = jsonParser.createLeague(user.username, user.password, name);

                if (leagueCreated)
                {
                    Response.Redirect("~/Account/Profile.aspx");
                }
                else
                {
                    // Feilmelding
                    league_exception.Text = "That league name is taken!";
                }
            }
            else
            {
                // Feilmelding
                league_exception.Text = "Your league needs a name!";
            }
           
        }

    }
}